package helpers

import (
	"go.mongodb.org/mongo-driver/bson/primitive"
)

func iterateMap(document map[string]interface{}, block func(map[string]interface{}, string)) {
	for key := range document {
		block(document, key)

		iterate(document[key], block)
	}
}

func iterate(i interface{}, block func(map[string]interface{}, string)) {
	switch typed := i.(type) {
	case primitive.A:
		iterateArray(typed, block)
	case []interface{}:
		iterateArray(typed, block)
	case primitive.M:
		iterateMap(typed, block)
	case map[string]interface{}:
		iterateMap(typed, block)
	default:
		return
	}
}

func iterateArray(typed []interface{}, block func(map[string]interface{}, string)) {
	for _, element := range typed {
		iterate(element, block)
	}
}
